package com.hello.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hello.dto.FollowDTO;
import com.hello.entity.Follow;
import com.hello.entity.PLikes;
import com.hello.mapper.FollowMapper;
import com.hello.mapper.PLikesMapper;
import com.hello.service.IFollowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Slf4j
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Override
    public List<Follow> getFollower(Long user_Id){
        QueryWrapper<Follow> wrapper = new QueryWrapper<Follow>()
                .select("*")
                    .eq("following_id",user_Id);
        List<Follow> follows = baseMapper.selectList(wrapper);
        return follows;
    }
    @Override
    public List<Follow> getFollowing(Long user_Id){
        QueryWrapper<Follow> wrapper = new QueryWrapper<Follow>()
                .select("*")
                .eq("follower_id",user_Id);
        List<Follow> follows = baseMapper.selectList(wrapper);
        return follows;
    }

    @Override
    public boolean addFollow(FollowDTO followDTO){
        Follow follow = new Follow();
        BeanUtils.copyProperties(followDTO,follow);
        follow.setChecked(false);
        follow.setFollowTime(LocalDateTime.now());
        int num = baseMapper.insert(follow);
        return num>0?true:false;
    }

    @Override
    public boolean haveFollowed(Long user_id,Long userb_id){
        QueryWrapper<Follow> wrapper = new QueryWrapper<Follow>()
                .select("*")
                .eq("follower_id",user_id)
                .eq("following_id",userb_id);
        boolean result = baseMapper.exists(wrapper);
        return result;
    }

    @Override
    public boolean deleteFollow(FollowDTO followDTO){
        QueryWrapper<Follow> wrapper = new QueryWrapper<>();
        wrapper.eq("follower_id",followDTO.getFollowerId())
                .eq("following_id",followDTO.getFollowingId()); //构建删除条件
        int num  = baseMapper.delete(wrapper);
        return num>0?true:false;
    }
}
